package com.company.project.modular.standard.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.model.Use;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.service.ITestRegistService;
import com.company.project.modular.system.model.Standard;
import com.company.project.modular.system.model.TestRegist;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.service.IUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实验对照登记控制器
 *
 * @author lsf
 * @Date 2018-10-30 15:42:15
 */
@Controller
@RequestMapping("/testRegist")
public class TestRegistController extends BaseController {

    private String PREFIX = "/standard/testRegist/";

    @Reference
    private ITestRegistService testRegistService;
    @Reference
    private IStandardService standardService;
    @Reference
    private IUseService useService;
    /**
     * 跳转到实验对照登记首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "testRegist.html";
    }

    /**
     * 跳转到添加实验对照登记
     */
    @RequestMapping("/testRegist_add")
    public String testRegistAdd() {
        return PREFIX + "testRegist_add.html";
    }

    /**
     * 获取标识码standardCode and standType = 1
     */
    @RequestMapping("/standardCode1")
    @ResponseBody
    public List<String> standardCode1(Model model) {
        int standardType = 1;
        /*查询标准物质表里面的标识码*/
        List<String> standardCodeOne = standardService.findByCode12(standardType);
        return standardCodeOne;
    }

    /**
     * 获取标识码standardCode and standType = 2
     */
    @RequestMapping("/standardCode2")
    @ResponseBody
    public List<String> standardCode2(Model model) {
        int standardType = 2;
        /*查询标准物质表里面的标识码*/
        List<String> standardCodeTwo = standardService.findByCode12(standardType);
        return standardCodeTwo;
    }
    /**
     * 添加实验对照登记
     */
    @RequestMapping("/standardCodeOne/{standardCodeOne}")
    @ResponseBody
    public Map<String,Object> standardCodeOne(@PathVariable String standardCodeOne, Standard standard, TestRegist testRegist) {
        standard = standardService.findByCodeOne(standardCodeOne);
        System.out.println("standard:"+standard);
        Map<String,Object> jsonObject = new HashMap<String, Object>();  //创建Json对象
       String standardName = standard.getStandardName();
        //设置Json对象的属性
        jsonObject.put("standardName", standardName);
        jsonObject.put("standardId", standard.getStandardId());
        jsonObject.put("standardLabel", standard.getStandardLabel());
        jsonObject.put("standardOrigin", standard.getStandardOrigin());
        jsonObject.put("standardCondition", standard.getStandardCondition());
        jsonObject.put("standardUse",  standard.getStandardUse());
        jsonObject.put("standardPath",  standard.getStandardPath());
        return jsonObject;
    }
    /**
     * 添加实验对照登记 standardCodeTwo
     */
    @RequestMapping("/standardCodeTwo/{standardCodeTwo}")
    @ResponseBody
    public Map<String,Object>  standardCodeTwo(@PathVariable String standardCodeTwo, Standard standard, TestRegist testRegist) {
        standard = standardService.findByCodeOne(standardCodeTwo);
        System.out.println("standard:"+standard);
        Map<String,Object> jsonObject = new HashMap<String, Object>();  //创建Json对象
        String standardName = standard.getStandardName();
        //设置Json对象的属性
        jsonObject.put("standardName", standardName);
        jsonObject.put("standardId", standard.getStandardId());
        jsonObject.put("standardLabel", standard.getStandardLabel());
        jsonObject.put("standardOrigin", standard.getStandardOrigin());
        jsonObject.put("standardCondition", standard.getStandardCondition());
        jsonObject.put("standardUse",  standard.getStandardUse());
        jsonObject.put("standardPath",  standard.getStandardPath());
        return jsonObject;
    }

    /**
     * 跳转到修改实验对照登记
     */
    @RequestMapping("/testRegist_update/{testRegistId}")
    public String testRegistUpdate(@PathVariable Integer testRegistId, Model model) {
        TestRegist testRegist = testRegistService.selectById(testRegistId);
        model.addAttribute("item",testRegist);
        LogObjectHolder.me().set(testRegist);
        return PREFIX + "testRegist_edit.html";
    }

    /**
     * 获取实验对照登记列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<TestRegist> list = testRegistService.selectList(null);
        /*List<Map<String,Object>> list = testRegistService.selectList1();*/
        return list;
    }

    /**
     * 新增实验对照登记
     * {standardCodeOne,standardCodeTwo}  不要这样写，一般是写api获取的时候才建议这样写 比如 /user/2   标识 获取id为2的user值
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TestRegist testRegist,Use use) {
        System.out.println("对照品标识码:" + testRegist.getTestCode());
        String testCode = testRegist.getTestCode();
        /*根据标识码去查use_state=0*/
        List<String> stateList = useService.findByState(testCode);
        if(stateList.size() != 0){
            /* 添加实验对照*/
            /*根据标识码，use_state=0查询id，并且根据id排序，选择第一个id存入到实验对照的useId里面*/
            List<String> idList = useService.findById(testCode);
           int id = Integer.valueOf(idList.get(0));
           testRegist.setUseId(id);
            //得到long类型当前时间
            long l = System.currentTimeMillis(); //new日期对象
            Date date = new Date(l);
            /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*///转换提日期输出格式
            testRegist.setTestTime(date);
            testRegistService.insert(testRegist);
            /* 编辑使用表,需要将state=0改为1*/
            use = useService.selectById(id);
            use.setUseState(1);
            useService.updateById(use);
            return SUCCESS_TIP;
        }else{
            return new ErrorTip(0, "该对照品尚未申请使用，请先申请使用");
        }
    }

    /**
     * 删除实验对照登记
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer testRegistId) {
        testRegistService.deleteById(testRegistId);
        return SUCCESS_TIP;
    }

    /**
     * 修改实验对照登记
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TestRegist testRegist,BindingResult bindingResult) {
        testRegistService.updateById(testRegist);
        return SUCCESS_TIP;
    }

    /**
     * 实验对照登记详情
     */
    @RequestMapping(value = "/detail/{testRegistId}")
    @ResponseBody
    public Object detail(@PathVariable("testRegistId") Integer testRegistId) {
        return testRegistService.selectById(testRegistId);
    }
}
